﻿using System.Collections.Generic;
using UnityEngine;

namespace GameFramework.skill.selector.impl
{
    public class SelectTargetSkillSelector:ISkillSelector
    {
        public void selectTarget(Skill skill, Transform skillPrefabInstance)
        {
             skill.targets.Clear();

            //根据技能数据中得标签 获取所有目标
            List<Transform> taragets = new List<Transform>();
            SkillConfig skillConfig = skill.skillConfig;
            List<GameObject> tempTagObject = new List<GameObject>();
            
            
            var targetTag = skillConfig.targetTag;
            int enimyTag = targetTag & 0x01;
            //TODO 计算出所有tag
            if (enimyTag != 0)
            {
                //TODO 找出周围有这个tag的对象，九宫格算法或其他算法
                tempTagObject.AddRange(GameObject.FindGameObjectsWithTag("Enimy"));
            }
            
            for (int i = 0; i < tempTagObject.Count; i++)
            {
                taragets.Add(tempTagObject[i].GetComponent<Transform>());
            }
            //判断攻击范围
            taragets = taragets.FindAll(t =>
                Vector3.Distance(t.position, skillPrefabInstance.position) <= skill.skillConfig.releaseRange 
            );
            if (taragets.Count == 0)
            {
                Debug.Log("没有敌人");
            }
            else
            {
                skill.targets.AddRange(taragets);
                for (int i = 0; i < taragets.Count; i++)
                {
                    Debug.Log("找到目标："+taragets[i].name);
                }
            }
        }
    }
}